﻿using Sidvall.UI.Dialogs;
using System.Windows;
using System.Windows.Controls;

namespace Sidvall.UI.Wpf.Controls
{
    public partial class MessageControl : UserControl
    {
        #region Public Members

        public event System.EventHandler AnswerSelected;

        #region ButtonMargin

        public static readonly System.Windows.DependencyProperty ButtonMarginProperty = System.Windows.DependencyProperty.Register(nameof(ButtonMargin), typeof(Thickness), typeof(MessageControl),
            new System.Windows.FrameworkPropertyMetadata(new Thickness(0, 8, 8, 8), System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Thickness ButtonMargin
        {
            get
            {
                return (Thickness)GetValue(ButtonMarginProperty);
            }
            set
            {
                SetValue(ButtonMarginProperty, value);
            }
        }

        #endregion
        #region InitializeContext

        public void InitializeContext(MessageContext message)
        {
            if (message == null)
                throw new System.ArgumentNullException("message");

            this.Message = message;
            this.txtMessage.Text = message.Message;
            if (message.MessageOptions.Count == 0)
                message.MessageOptions.AddOk(true, true);
            this.ButtonPanel.Children.Clear();
            foreach (var option in message.MessageOptions)
            {
                var button = new Button();
                button.Content = option.Message;
                button.Padding = new Thickness(15, 3, 15, 3);
                button.MinWidth = 70;
                if (this.ButtonPanel.Children.Count > 0)
                    button.Margin = new Thickness(8, 0, 0, 0);
                if (message.MessageOptions.DefaultMessageOptionId == option.Id)
                    button.IsDefault = true;
                if (message.MessageOptions.CancelMessageOptionId == option.Id)
                    button.IsCancel = true;
                button.Tag = option;
                button.Click += new RoutedEventHandler(button_Click);
                this.ButtonPanel.Children.Add(button);
            }
        }

        #endregion

        #endregion
        #region Private Members

        private MessageContext Message { get; set; }

        #region OnAnswerSelected

        private void OnAnswerSelected()
        {
            OnAnswerSelected(new System.EventArgs());
        }
        private void OnAnswerSelected(System.EventArgs args)
        {
            OnAnswerSelected(this, args);
        }
        private void OnAnswerSelected(object sender, System.EventArgs args)
        {
            if (this.AnswerSelected != null)
                this.AnswerSelected.Invoke(sender, args);
        }

        #endregion

        #endregion
        #region Events

        #region button_Click

        private void button_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            var option = button.Tag as MessageOption;
            this.Message.Answers.Add(option);
            OnAnswerSelected();
        }

        #endregion

        #endregion
        #region Constructors

        public MessageControl()
        {
            InitializeComponent();
        }

        #endregion
    }
}
